#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/5 13:37
# @Author  : Andrewq
# @Site    : nope
# @File    : orderResource.py
# @Software: PyCharm

from flask import jsonify, request
from ..models.OrderInfoModel import OrderInfoManager
from ..utils.response_code import RET
from ..models.NewspaperOrderMainView import NewspaperOrderMainView


class OrderResource:

    @classmethod
    def get_department_order_statistics(cls):

        result_dict = OrderInfoManager.get_department_order_statistics()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_order_all_info(cls):

        result_dict = OrderInfoManager.get_order_info_by_tags()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def add_department_order_info(cls):
        datas = eval(request.form.get('datas'))

        if not datas:
            return jsonify(code=RET.PARAMERR, msg='缺失参数datas', data=[])

        result_dict = OrderInfoManager.add_department_order_info(**{'datas': datas})

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def add_employee_order_info(cls):
        datas = eval(request.form.get('datas'))

        if not datas:
            return jsonify(code=RET.PARAMERR, msg='缺失参数datas', data=[])

        result_dict = OrderInfoManager.add_employee_order_info(**{'datas': datas})

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def delete_order_info_list(cls):
        datas = eval(request.form.get('datas'))

        if not datas:
            return jsonify(code=RET.PARAMERR, msg='缺失参数datas', data=[])

        result_dict = OrderInfoManager.delete_order_info_list(**{'datas': datas})

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def edit_order_info(cls):
        pass

    @classmethod
    def get_order_info_by_tags(cls):
        tag_name = request.args.get('tag')
        keyword = request.args.get('keyword')

        if all([tag_name, keyword]):
            args = {
                'tag': tag_name,
                'keyword': keyword
            }
        else:
            args = {}

        result_dict = OrderInfoManager.get_order_info_by_tags(**args)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_order_info_by_newspaper_ID(cls):
        newspaperID = request.args.get('newspaperID')
        tag_name = request.args.get('tag')
        keyword = request.args.get('keyword')

        if newspaperID is None:
            return jsonify(code="500", message="缺少参数newspaperID")

        if all([tag_name, keyword]):
            if tag_name == "用户ID":
                args = {
                    'NewspaperID': newspaperID,
                    'tag': "UserID",
                    'keyword': keyword
                }
            elif tag_name == "部门ID":
                args = {
                    'NewspaperID': newspaperID,
                    'tag': "DepartmentID",
                    'keyword': keyword
                }
            elif tag_name == "用户名":
                args = {
                    'NewspaperID': newspaperID,
                    'tag': "UserName",
                    'keyword': keyword
                }
            elif tag_name == "部门名":
                args = {
                    'NewspaperID': newspaperID,
                    'tag': "DepartmentName",
                    'keyword': keyword
                }
            else:
                args = {
                    'NewspaperID': newspaperID,
                    'tag': tag_name,
                    'keyword': keyword
                }
        else:
            args = {'NewspaperID': newspaperID}

        result_dict = NewspaperOrderMainView.get_order_info_by_newspaper_ID(**args)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))